﻿using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CatUtils.Utils.Currency
{/// <summary>
/// List帮助类
/// </summary>
    public class ListHelper
    {
        /// <summary>
        /// List去重复
        /// </summary>
        /// <param name="OldList"></param>
        /// <returns></returns>
        public static List<string> ListRepeat(List<string> OldList)
        {
            List<string> ToRepeat = new List<string>();

            foreach (string line in OldList)
            {
                if (!TextHelper.JudgeNull(line) && !ToRepeat.Contains(line) && line != "")
                    ToRepeat.Add(line);
            }
            return ToRepeat;
        }

        /// <summary>
        /// 替换整个List里的内容  增强模式，使用正则
        /// </summary>
        /// <param name="RepList"></param>
        /// <param name="ReplaceForm"></param>
        /// <param name="ReplaceTo"></param>
        /// <returns></returns>
        public static List<string> RepListEnhanceHelper(List<string> RepList, string ReplaceForm, string ReplaceTo)
        {
            List<string> TempList = new List<string>();
            foreach (string Line in RepList)
            {
                if (Line == "")
                    continue;
                TempList.Add(TextHelper.ReplaceText(Line, ReplaceForm, ReplaceTo));
            }

            return TempList;
        }

        /// <summary>
        /// 判断元素是否存在
        /// </summary>
        /// <param name="SourceList"></param>
        /// <param name="DestList"></param>
        /// <returns></returns>

        public static bool Exit(List<long> SourceList, List<long> DestList)
        {
            foreach (var item in DestList)
            {
                if (SourceList.Exists(count => count == item))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 判断元素是否存在
        /// </summary>
        /// <param name="SourceList"></param>
        /// <param name="DestList"></param>
        /// <returns></returns>

        public static bool Exit(List<string> SourceList, List<string> DestList)
        {
            foreach (var item in DestList)
            {
                if (SourceList.Exists(count => count == item))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 判断元素是否存在
        /// </summary>
        /// <param name="SourceList"></param>
        /// <param name="DestList"></param>
        /// <returns></returns>

        public static bool Exit(List<int> SourceList, List<int> DestList)
        {
            foreach (var item in DestList)
            {
                if (SourceList.Exists(count => count == item))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 判断元素是否存在
        /// </summary>
        /// <param name="SourceList"></param>
        /// <param name="DestList"></param>
        /// <returns></returns>

        public static bool Exit(List<double> SourceList, List<double> DestList)
        {
            foreach (var item in DestList)
            {
                if (SourceList.Exists(count => count == item))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 逐行判断是否包含屏蔽词，如果包含则移除。
        /// </summary>
        /// <param name="TextList"></param>
        /// <param name="ExcludeList"></param>
        /// <returns></returns>
        public static List<string> ListRemoveExclude(List<string> TextList, List<string> ExcludeList)
        {
            List<string> TempList = new List<string>();
            int Count = 0;
            foreach (string Line in TextList)
            {
                new PrintLog().PercentLog(TextList.Count, ++Count);
                bool ContinueFlag = false;
                foreach (string Exclude in ExcludeList)
                {
                    if (Exclude != "" && Line.IndexOf(Exclude) >= 0)
                    {
                        Console.WriteLine("捕获到关键词" + Exclude);

                        ContinueFlag = true;
                        break;
                    }
                }
                if (ContinueFlag)
                    continue;
                TempList.Add(Line);
            }
            return TempList;
        }

        /// <summary>
        /// 切割list，返回最后面的的指定数量
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="NarrowCount"></param>
        /// <returns></returns>
        public static List<string> ListNarrow(string FileName, int NarrowCount)
        {
            List<string> FileAllLine = FileHelper.ReadAllLine(FileName);
            FileAllLine = FileAllLine.Skip(FileAllLine.Count - NarrowCount).ToList<string>();
            Console.WriteLine("日志处理后剩余{0}行", FileAllLine.Count);
            return FileAllLine;
        }

        /// <summary>
        /// 按指定数字切割list，并返回封装的List。例如：传入1000个数组，按一百一组，分割十组并且返回。如果想获取第一个请配合List自带方法：FirstOrDefault()使用
        /// </summary>
        /// <param name="DataList"></param>
        /// <param name="SkipCount"></param>
        /// <returns></returns>
        public static List<List<string>> ListSkip(List<string> DataList, int SkipCount)
        {
            List<List<string>> TempData = new List<List<string>>();

            int size = (int)Math.Ceiling((decimal)DataList.Count / SkipCount);
            for (int i = 1; i <= size; i++)
            {
                TempData.Add(DataList.Skip((i - 1) * SkipCount).Take(SkipCount).ToList());
            }

            return TempData;
        }

        /// <summary>
        /// 指定一个规则 替换整个LIST里的内容
        /// </summary>
        /// <param name="TextList"></param>
        /// <param name="RegexPattern"></param>
        /// <param name="NewText"></param>
        /// <returns></returns>
        public static List<string> ReplaceList(List<string> TextList, string RegexPattern, string NewText = "")
        {
            List<string> ImgTemp = new List<string>();

            foreach (string Line in TextList)
            {
                if (Line == "")
                    continue;
                ImgTemp.Add(TextHelper.ReplaceText(Line, RegexPattern, NewText));
            }
            return ImgTemp;
        }

        /// <summary>
        /// 从LIST里随机抽取一个结果返回 如果没有结果则返回null
        /// </summary>
        /// <param name="TextList"></param>
        /// <returns></returns>
        public static object RandomResult(List<object> TextList)
        {
            if (TextList.Count == 0)
                return null;
            int Random = new Random().Next(TextList.Count);

            return TextList[Random];
        }

        /// <summary>
        /// 从LIST里随机抽取一个结果返回 如果没有结果则返回空字符串
        /// </summary>
        /// <param name="TextList"></param>
        /// <returns></returns>
        public static string RandomResult(List<string> TextList)
        {
            if (TextList.Count == 0)
                return "";
            int Random = new Random().Next(TextList.Count);

            return TextList[Random];
        }

        /// <summary>
        /// 从LIST里随机抽取一个结果返回 如果没有结果则返回 -100001
        /// </summary>
        /// <param name="TextList"></param>
        /// <returns></returns>
        public static int RandomResult(List<int> TextList)
        {
            if (TextList.Count == 0)
                return -100001;
            int Random = new Random().Next(TextList.Count);

            return TextList[Random];
        }

        /// <summary>
        /// 打乱List数组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ListT"></param>
        /// <returns></returns>
        public static List<T> RandomSort<T>(List<T> ListT)
        {
            List<T> newList = new List<T>();
            foreach (T item in ListT) { newList.Insert(TextHelper.GetRandom(newList.Count), item); }
            return newList;
        }

        /// <summary>
        ///  判断Sublist中的字符串，是否有ParentList的关键词。 如 ParentList关键词 word 会匹配Sublist中的： wordPress, keywords  。若包含关键词返回true
        /// </summary>
        /// <param name="Sublist"></param>
        /// <param name="ParentList"></param>
        /// <returns></returns>
        public static bool CompareRepetition(List<string> Sublist, List<string> ParentList)
        {
            return CompareRepetition(Sublist, ParentList, false);
        }

        /// <summary>
        ///  判断Sublist中的字符串，是否有ParentList的关键词。 如 ParentList关键词 word 会匹配Sublist中的： wordPress, keywords。若包含关键词返回true
        /// </summary>
        /// <param name="Sublist"></param>
        /// <param name="ParentList"></param>
        /// <param name="OutLog"></param>
        /// <returns></returns>
        public static bool CompareRepetition(List<string> Sublist, List<string> ParentList, bool OutLog)
        {
            foreach (string line in Sublist)
            {
                if (CompareRepetition(line, ParentList, OutLog))
                    return true;
            }
            return false;
        }

        /// <summary>
        ///  判断字符串中是否包含关键词， 如 关键词 word 会匹配： wordPress, keywords。包含关键词返回true
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="ExcluedeList"></param>
        /// <returns></returns>
        public static bool CompareRepetition(string Key, List<string> ExcluedeList)
        {
            return CompareRepetition(Key, ExcluedeList, false);
        }

        /// <summary>
        /// 判断字符串中是否包含关键词， 如 关键词 word 会匹配： wordPress, keywords。包含关键词返回true
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="ExcluedeList"></param>
        /// <param name="OutLog"></param>
        /// <returns></returns>
        public static bool CompareRepetition(string Key, List<string> ExcluedeList, bool OutLog)
        {
            foreach (var item in ExcluedeList)
            {
                if (item == "")
                    continue;
                if (Key.IndexOf(item) >= 0)
                {
                    if (OutLog)
                        Console.WriteLine($"{Key}被关键词{item}被屏蔽。");
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// 判断ExcluedeList是否包含KeyList的关键词，如果有任意关键词完全相等则返回true
        /// </summary>
        /// <param name="KeyList"></param>
        /// <param name="ExcluedeList"></param>
        /// <returns></returns>
        public static bool EqualsList(List<string> KeyList, List<string> ExcluedeList)
        {
            return EqualsList(KeyList, ExcluedeList, true);
        }

        /// <summary>
        /// 判断ExcluedeList是否包含KeyList的关键词，如果有任意关键词完全相等则返回true
        /// </summary>
        /// <param name="KeyList"></param>
        /// <param name="ExcluedeList"></param>
        /// <param name="OutLog">是否打印日志</param>
        /// <returns></returns>
        public static bool EqualsList(List<string> KeyList, List<string> ExcluedeList, bool OutLog)
        {
            foreach (var Key in KeyList)
            {
                if (EqualsList(Key, ExcluedeList, OutLog))
                    return true;
            }
            return false;
        }

        /// <summary>
        /// 判断ExcluedeList是否包含Key，如果完全相等则返回true
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="ExcluedeList"></param>
        /// <returns></returns>
        public static bool EqualsList(string Key, List<string> ExcluedeList)
        {
            return EqualsList(Key, ExcluedeList, true);
        }

        /// <summary>
        ///判断ExcluedeList是否包含Key，如果完全相等则返回true
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="ExcluedeList"></param>
        /// <param name="OutLog"></param>
        /// <returns></returns>
        public static bool EqualsList(string Key, List<string> ExcluedeList, bool OutLog)
        {
            if (ExcluedeList.Contains(Key))
            {
                if (OutLog)
                {
                    Console.WriteLine($"关键词{Key}被屏蔽。");
                }
                return true;
            }

            return false;
        }

        /// <summary>
        /// 获取列表的第一个值 如果没有值返回""
        /// </summary>
        /// <param name="ValueList"></param>
        /// <returns>如果没有值返回""</returns>
        public static string GetFirst(List<string> ValueList)
        {
            if (ValueList == null || ValueList.Count == 0)
                return "";
            return ValueList.FirstOrDefault();
        }

        /// <summary>
        /// 获取列表的第一个值 如果没有值返回0
        /// </summary>
        /// <param name="ValueList"></param>
        /// <returns>如果没有值返回0</returns>
        public static int GetFirst(List<int> ValueList)
        {
            if (ValueList == null || ValueList.Count == 0)
                return 0;
            return ValueList.FirstOrDefault();
        }

        /// <summary>
        /// 获取列表的最后一个值 如果没有值返回""
        /// </summary>
        /// <param name="ValueList"></param>
        /// <returns>如果没有值返回""</returns>
        public static string GetLast(List<string> ValueList)
        {
            if (ValueList == null || ValueList.Count == 0)
                return "";
            return ValueList.LastOrDefault();
        }

        /// <summary>
        /// 获取列表的第一个值 如果没有值返回0
        /// </summary>
        /// <param name="ValueList"></param>
        /// <returns>如果没有值返回0</returns>
        public static int GetLast(List<int> ValueList)
        {
            if (ValueList == null || ValueList.Count == 0)
                return 0;
            return ValueList.LastOrDefault();
        }

        /// <summary>
        /// String字符串排序 默认从短到长
        /// </summary>
        /// <param name="TextList"></param>
        /// <returns></returns>
        public static List<string> Sort(List<string> TextList)
        {
            return Sort(TextList, true);
        }

        /// <summary>
        /// String字符串排序
        /// </summary>
        /// <param name="TextList"></param>
        /// <param name="ShortToLong">由短字符串到长字符串</param>
        /// <returns></returns>
        public static List<string> Sort(List<string> TextList, bool ShortToLong)
        {
            if (ShortToLong)
                TextList.Sort((a, b) => a.Length.CompareTo(b.Length));
            else
                TextList.Sort((a, b) => b.Length.CompareTo(a.Length));
            return TextList;
        }

        /// <summary>
        ///数字List排序 由大到小
        /// </summary>
        /// <param name="NumberList"></param>
        /// <returns></returns>
        public static List<int> Sort(List<int> NumberList)
        {
            return Sort(NumberList, true);
        }

        /// <summary>
        /// 数字List排序
        /// </summary>
        /// <param name="NumberList"></param>
        /// <param name="SmallToLarge">从小到大</param>
        /// <returns></returns>
        public static List<int> Sort(List<int> NumberList, bool SmallToLarge)
        {
            NumberList.Sort();
            if (!SmallToLarge)
                NumberList.Reverse();

            return NumberList;
        }
    }
}